//
//  ZMultiChosePanelCell.m
//  WeClubs
//
//  Created by Zed Link on 28/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZMultiChosePanelCell.h"
#import "UIButton+ZUtilities.h"
#import "UIImage+ZUtilities.h"
#import "ZResourceManager.h"

NSString * const MultiChosePanelItemReuseID = @"MultiChosePanelItemReuseID";

@interface ZMultiChosePanelCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation ZMultiChosePanelCell

- (void)zSetupCell
{
    [super zSetupCell];
    [self.contentView setBackgroundColor:NAVBAR_BG_COLOR];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    layout.itemSize = CGSizeMake(64, 28);
    layout.minimumLineSpacing = 12;
    layout.minimumInteritemSpacing = 4;
    
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
    [self.collectionView registerClass:[ZMultiChosePanelItem class] forCellWithReuseIdentifier:MultiChosePanelItemReuseID];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZMultiChoseModel *model = self.itemModels[indexPath.row];
    
    ZMultiChosePanelItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:MultiChosePanelItemReuseID forIndexPath:indexPath];
    [item.button zCornerDeal:14];
    item.state = model.selected ? ZMultiChoseStateSelected : ZMultiChoseStateNormal;
    
    if (indexPath.row >= self.itemModels.count - 1) {
        [item setTitle:model.itemName andFontSize:19];
    }
    else {
        [item setTitle:model.itemName andFontSize:14];
    }
    
    return item;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZMultiChosePanelItem *item = (ZMultiChosePanelItem *)[collectionView cellForItemAtIndexPath:indexPath];
    
    if (indexPath.row == self.itemModels.count - 1) {
        if ([self.delegate respondsToSelector:@selector(multiChosePanelCell:didSelectedPlusItem:)]) {
            [self.delegate multiChosePanelCell:self didSelectedPlusItem:item.button];
        }
    }
    else {
        item.state = !item.state;
        if ([self.delegate respondsToSelector:@selector(multiChosePanelCell:didSelectedItem:AtIndex:)]) {
            [self.delegate multiChosePanelCell:self didSelectedItem:item.state AtIndex:indexPath.row];
        }
    }
}

#pragma mark - Getter & Setter

- (void)setItemModels:(NSArray<ZMultiChoseModel *> *)itemModels
{
    ZMultiChoseModel *model = [[ZMultiChoseModel alloc] init];
    model.itemName = @"+";
    model.selected = NO;
    
    _itemModels = [itemModels arrayByAddingObject:model];
    // 刷新collectionView, 调整其高度约束
    [self.collectionView reloadData];
    
    CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
}

@end




@implementation ZMultiChosePanelItem

- (void)zSetupCell
{
    [super zSetupCell];
    
    UIButton *button = [UIButton zButtonWithTitle:@"Button" fontSize:14 textColor:FONT_THIRD_COLOR];
    button.userInteractionEnabled = NO;
    
    [button setTitleColor:FONT_THIRD_COLOR forState:UIControlStateNormal];
    [button setBackgroundColor:SEPARATOR_BG_COLOR];
    
    self.button = button;
    [self.contentView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)setTitle:(NSString *)title andFontSize:(CGFloat)fontSize
{
    self.button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    [self.button setTitle:title forState:UIControlStateNormal];
}

- (void)setState:(ZMultiChoseState)state
{
    _state = state;
    
    switch (_state) {
        case ZMultiChoseStateNormal:
        {
            [self.button setTitleColor:FONT_THIRD_COLOR forState:UIControlStateNormal];
            [self.button setBackgroundColor:SEPARATOR_BG_COLOR];
        }
            break;
        case ZMultiChoseStateSelected:
        {
            [self.button setTitleColor:NAVBAR_BG_COLOR forState:UIControlStateNormal];
            [self.button setBackgroundColor:HIGHTLIGHT_COLOR];
        }
            break;
        default:
            break;
    }
}

@end
